package xyz.erupt.flow.bean.entity.form;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data;
import xyz.erupt.core.exception.EruptApiErrorTip;
import xyz.erupt.flow.core.engine.formItems.vo.FormItemFieldVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author PP
 * @version 1.0
 * @project Erupt
 * @description
 * @date 2024/9/20 08:57:47
 */
@Data
public class FormComponent {
    private String title;
    private String name;
    private String icon;
    @JsonDeserialize(using = CustomValueDeserializer.class)
    private Object value; // 可以是数组或对象
    private String valueType;
    private Props props;
    private String id;
    // 根据 ID 递归查询 Name
    public static String findNameById(List<FormComponent> components, String id) {
        for (FormComponent component : components) {
            if (component.getId().equals(id)) {
                return component.getName();
            }
            if (component.getProps() != null) {
                // 查找 items
                if (component.getProps().getItems() != null) {
                    String name = findNameById(component.getProps().getItems(), id);
                    if (name != null) {
                        return name;
                    }
                }
                // 查找 columns
                if (component.getProps().getColumns() != null) {
                    String name = findNameById(component.getProps().getColumns(), id);
                    if (name != null) {
                        return name;
                    }
                }
            }
        }
        return null;
    }
    // 根据 ID 递归查询 title
    public static String findTitleById(List<FormComponent> components, String id) {
        for (FormComponent component : components) {
            if (component.getId().equals(id)) {
                return component.getTitle();
            }
            if (component.getProps() != null) {
                // 查找 items
                if (component.getProps().getItems() != null) {
                    String title = findTitleById(component.getProps().getItems(), id);
                    if (title != null) {
                        return title;
                    }
                }
                // 查找 columns
                if (component.getProps().getColumns() != null) {
                    String title = findTitleById(component.getProps().getColumns(), id);
                    if (title != null) {
                        return title;
                    }
                }
            }
        }
        return null;
    }

    /**
     * 查询分栏数据的所有子类型（递归查询）
     * @return
     */
    public List<FormComponent> findAllItemFieldsBySpanLayout() {
        ArrayList<FormComponent> itemFieldVos = new ArrayList<>();
        if(this.name.equals("SpanLayout")){//是spanLayout类型数据，继续搜索
            //搜索props属性
            if(props!=null && !props.getItems().isEmpty()){
                props.getItems().forEach(item -> {
                    addSubItemFields(item,itemFieldVos);//想列表递归添加数据
                });
            }
        }//不是则不搜索
        return itemFieldVos;
    }

    private void addSubItemFields(FormComponent item, ArrayList<FormComponent> itemFieldVos) {
        if(item.name.equals("SpanLayout")){
            if(item.props != null && !item.props.getItems().isEmpty()){//存在则继续查找
                addSubItemFields(item,itemFieldVos);
            }
        }else {//不是该类型的则直接添加到列表返回
            itemFieldVos.add(item);
        }
    }
}
